﻿@using System.Text
@using OA.Model
@using OA.AspNetCore.Html
@using OA.Core.Model

@model OA.Model.ApprovalNode
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>@ViewBag.Title</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="@ViewBag.OssUrl/front/css/admin.css" />
    <link rel="stylesheet" href="@ViewBag.OssUrl/front/layui/css/layui.css">
    <script type="text/javascript" src="@ViewBag.OssUrl/js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="@ViewBag.OssUrl/front/layui/layui.js"></script>
    <script type="text/javascript">
        var parentUrl = "";
        var currentUrl = "";
    </script>
</head>
<body>
    <style type="text/css">
        .myDepartment .layui-input-inline {
            margin-bottom: 10px;
        }

        .layui-btn .layui-icon {
            margin-right: 0px;
            margin-left: 6px;
        }
    </style>
    <div class="fly-panel-user" style="padding:20px 20px 20px 20px;">


        @{
            BaseConfig config = ViewBag.BaseConfig;
            ViewBag.Title = "";
            Layout = null;

            string msg = ViewBag.Msg;
            string tips = string.Empty;
            if (!string.IsNullOrWhiteSpace(msg))
            {
                tips = $"layer.msg('{msg}');";
            }
            Dictionary<string, List<ListItem>> attributeGroup = ViewBag.AllAttribute;

            attributeGroup["基础数据"].Add(new ListItem() { Value = "DepartmentID_1", Text = "一级部门(int)" });
            attributeGroup["基础数据"].Add(new ListItem() { Value = "DepartmentID_2", Text = "二级部门(int)" });
            attributeGroup["基础数据"].Add(new ListItem() { Value = "DepartmentID_3", Text = "三级部门(int)" });
            attributeGroup["基础数据"].Add(new ListItem() { Value = "DepartmentID_4", Text = "四级部门(int)" });
            attributeGroup["基础数据"].Add(new ListItem() { Value = "VirtualPositionLevel", Text = "虚拟职层(String)" });
            attributeGroup["基础数据"].Add(new ListItem() { Value = "Approver", Text = "审批人工号(String)" });


            List<ListItem> attributes = new List<ListItem>();

            foreach (var item in attributeGroup)
            {
                attributes.Add(new ListItem() { Text = "---------------" + item.Key + "---------------", Value = "" });
                attributes.AddRange(item.Value);
            }


            List<string> keywordQ = new List<string>();
            if (!string.IsNullOrWhiteSpace(Model.EnableEditableFields))
            {
                keywordQ = Model.EnableEditableFields.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }

            List<string> keywordQ2 = new List<string>();
            if (!string.IsNullOrWhiteSpace(Model.PrivacyFields))
            {
                keywordQ2 = Model.PrivacyFields.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }

            ApprovalTree approvalTree = ViewBag.ApprovalTree;
            List<ListItem> positions = ViewBag.Positions;

        }
        <div class="fly-panel fly-panel-user" pad20="">
            <div class="layui-tab layui-tab-brief" lay-filter="user">
                <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding:20px 40px 20px 0;">
                    @using (Html.BeginForm(FormMethod.Post, htmlAttributes: new { @class = "layui-form", id = "myForm" }))
                    {
                        @Html.AntiForgeryToken()

                        <div class="layui-form-item">
                            @Html.LabelFor(model => model.Name, new { @class = "layui-form-label" })
                            <div class="layui-input-block">
                                @Html.HiddenFor(model => model.ID)
                                @Html.HiddenFor(model => model.ParentNodeID)
                                @Html.HiddenFor(model => model.CustomFormID)
                                @Html.HiddenFor(model => model.IsPre)
                                @Html.MyEditorFor(model => model.Name)

                                <input type="hidden" value="@ViewBag.OldPositionID" name="OldPositionID" />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            @Html.LabelFor(model => model.TypeID, new { @class = "layui-form-label" })
                            <div class="layui-input-block">
                                @Html.MyEditorFor(model => model.TypeID)
                            </div>
                        </div>
                        @if (Model.CustomFormID == 0)
                        {
                        <script type="text/javascript">
                            $("#TypeID option").last().remove();                            
                        </script>
                        }
                        <div class="layui-form-item">
                            @Html.LabelFor(model => model.IsDraftsNode, new { @class = "layui-form-label" })
                            <div class="layui-input-block">
                                @Html.MyEditorFor(model => model.IsDraftsNode)
                            </div>
                        </div>


                        <div class="layui-form-item">
                            @Html.LabelFor(model => model.DepartmentID, new { @class = "layui-form-label" })
                            <div class="layui-input-block myDepartment">

                            </div>
                        </div>

                        <div class="layui-form-item">
                            @Html.LabelFor(model => model.PositionID, new { @class = "layui-form-label" })
                            <div class="layui-input-block">
                                @Html.MyEditorFor(model => model.PositionID)
                            </div>
                        </div>



                        @if (Model.CustomFormID != 0)
                        {
                            <div class="layui-form-item">
                                @Html.LabelFor(model => model.Countersign, new { @class = "layui-form-label" })
                                <div class="layui-input-block">
                                    <div id="Countersign" class="demo-transfer"></div>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">可编辑字段</label>
                                <div class="layui-input-block">
                                    @foreach (var key in attributeGroup.Keys)
                                    {
                                        if (key == "基础数据") { continue; }
                                        List<ListItem> list = attributeGroup[key];
                                        <fieldset class="layui-elem-field layui-field-title" style="margin: 5px 0;">
                                            <legend style="font-size:13px;color:red">@key</legend>
                                        </fieldset>
                                        foreach (var item in list)
                                        {
                                            if (item.Value == "Sponsor" || item.Value == "PositionID" || item.Value == "DepartmentID" || item.Value.EndsWith("__Total"))
                                            {
                                                continue;
                                            }
                                            if (keywordQ.Contains(item.Value))
                                            {
                                                <input type="checkbox" name="EditableField" lay-skin="primary" title="@item.Text" value="@item.Value" checked>
                                            }
                                            else
                                            {
                                                <input type="checkbox" name="EditableField" lay-skin="primary" title="@item.Text" value="@item.Value">
                                            }
                                        }

                                    }
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">隐私字段</label>
                                <div class="layui-input-block">
                                    @foreach (var key in attributeGroup.Keys)
                                    {
                                        if (key == "基础数据") { continue; }
                                        List<ListItem> list = attributeGroup[key];
                                        <fieldset class="layui-elem-field layui-field-title" style="margin: 5px 0;">
                                            <legend style="font-size:13px;color:red">@key</legend>
                                        </fieldset>
                                        foreach (var item in list)
                                        {
                                            if (item.Value == "Sponsor" || item.Value == "PositionID" || item.Value == "DepartmentID" || item.Value == "__Total")
                                            {
                                                continue;
                                            }
                                            if (keywordQ.Contains(item.Value))
                                            {
                                                <input type="checkbox" name="PrivacyField" lay-skin="primary" title="@item.Text" value="@item.Value" checked>
                                            }
                                            else
                                            {
                                                <input type="checkbox" name="PrivacyField" lay-skin="primary" title="@item.Text" value="@item.Value">
                                            }
                                        }

                                    }
                                </div>
                            </div>



                            <div class="layui-form-item">
                                <label class="layui-form-label">条件过滤</label>
                                <div class="layui-input-block">
                                    <table class="layui-table">
                                        <thead>
                                            <tr>
                                                <td colspan="7" style="color:red;text-align:center;">关于分组说明,系统会根据分组值进行分组，相同分组值的条件为一组，组内条件之间以 and 连接；不同组之间通过 or 连接</td>
                                            </tr>
                                            <tr>
                                                <th style="width:40px;">分组</th>
                                                <th>属性名称</th>
                                                <th style="width:140px;">对比类型</th>
                                                <th style="width: 240px;">值(Value)</th>
                                                <th style="width:40px;">#</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @for (int i = 0; i < Model.ListCondition.Count; i++)
                                            {
                                                <tr data-id="@i">
                                                    <td>
                                                        <input type="text" value="@Model.ListCondition[i].GroupID" name="ListCondition[@i].GroupID" class="layui-input GroupID" />
                                                    </td>
                                                    <td>
                                                        <input type="hidden" class="Status" name="ListCondition[@i].Status" value="1" />
                                                        @Html.MyEditorFor(model => model.ListCondition[i].KeyWord, "ListCondition[" + i.ToString() + "].", attributes)
                                                    </td>
                                                    <td>
                                                        @Html.MyEditorFor(model => model.ListCondition[i].TypeID, "ListCondition[" + i.ToString() + "].")
                                                    </td>
                                                    <td>
                                                        <input type="text" value="@Model.ListCondition[i].Value" name="ListCondition[@i].Value" class="layui-input Value" />
                                                    </td>
                                                    <td>
                                                        <span class="deleteItem">删除</span>
                                                    </td>
                                                </tr>
                                            }
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <td colspan="7" class="right_content"><button type="button" class="layui-btn layui-btn-normal addCondition">增加一行</button></td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        }




                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <input type="submit" value="提 交" lay-submit="" lay-filter="OAForm" class="layui-btn mySubmit" />
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="/Member/Home/GetDepartmentAndPosition"></script>
    <script type="text/javascript" src="@config.OssUrl/front/js/department.js"></script>
    <script type="text/javascript">
    layui.use(['form', 'layedit', 'element', 'layer', 'util','transfer'], function () {
		form = layui.form;
        layer = layui.layer;
        element = layui.element;
        transfer = layui.transfer;
        util = layui.util;
        @Html.Raw(tips)
        form.on('select(Department)', function (data) {
            InitDepartment($(".myDepartment"), data.value);
            InitPosition($("#PositionID"), data.value, 0);
            form.render('select');
        });
        form.on('submit(OAForm)', function (data) {
            $(".mySubmit").prop("disabled", true);
            var index = layer.load(1, {
                shade: [0.1, '#fff']
            });
            var myFrom = $("#myForm");
            $.post(myFrom.prop("action"), myFrom.serialize(), function (result) {
                $(".mySubmit").prop("disabled", false);
                layer.closeAll();
                if (result.State == 400) {
                    layer.msg(result.Msg);
                    return;
                }
                if (result.State == 402) {
                    layer.msg(result.Msg);
                    return;
                }
                else {
                    layer.msg(result.Msg);
                    window.parent.location.href = window.parent.location.href;
                }
            });
            return false;
        });
        InitDepartment($(".myDepartment"), @Model.DepartmentID);
        InitPosition($("#PositionID"), @Model.DepartmentID,@Model.PositionID);
        form.render('select');
        var data =@Html.Raw(ViewBag.Options);
        transfer.render({
            elem: '#Countersign'
            , data: data
            , title: ['所有岗位', '会签岗位']
            , showSearch: true
            , name: 'Countersign'
            , value: @Html.Raw(Model.Countersign)
        });

        form.on('submit(demo1)', function (data) {
            $("#layTransferRightCheckAll").parent().parent().find("ul li input").prop("checked", true);
            form.render();
            return true;
        });

     });
    $(".addCondition").click(function () {
        var my_tbody = $(this).parent().parent().parent().parent().find("tbody");
        var last = my_tbody.find("tr:last");
        var lastID = last.attr("data-id");
        var currentIndex = parseInt(lastID) + 1;
        var outerHTML = last.prop('outerHTML');
        var reg = new RegExp("\\[" + lastID + "\\]", "g");
        outerHTML = outerHTML.replace('data-id="' + lastID + '"', 'data-id="' + currentIndex + '"');
        outerHTML = outerHTML.replace(reg, "[" + currentIndex + "]");
        my_tbody.append(outerHTML);
        my_tbody.find("tr:last").css("display", "");
        layui.use('form', function () {
            var form = layui.form;
            form.render();
        });
    });
    $(document).on("click", ".deleteItem", function () {
        $(this).parent().parent().css("display", "none");
        $(this).parent().parent().find(".Status").val("-1");
    });
    </script>
</body>
</html>

